package com.tx.admin.core.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.tx.admin.core.entity.SuperEntity;
import com.tx.admin.core.service.SuperService;
import com.tx.admin.core.anno.RbacMapping;
import com.tx.admin.core.po.EntityPo;
import com.tx.admin.core.po.PagePo;
import com.tx.admin.core.vo.EntityVo;
import com.tx.admin.core.vo.PageVo;
import com.tx.admin.util.common.GenericUtil;

import io.swagger.annotations.ApiOperation;

public abstract class SuperController<E extends SuperEntity> {

	@Autowired
	private SuperService<E> superService;
	

	@ApiOperation("导出数据")
	@GetMapping("/exportExcell")
	public void exportExcell(HttpServletResponse response) throws IOException {
	    List<E> list = this.superService.findAll();
		ServletOutputStream out = response.getOutputStream();
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLS, true);
        String fileName =this.getEntityClass().getSimpleName();
        Sheet sheet = new Sheet(1, 0,this.getEntityClass());
        writer.write(list, sheet);
        
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename="+fileName+".xls");
        writer.finish();
        out.flush();
	}
	
	@ApiOperation("导入excell文件")
	@PostMapping("/readExcell")
	public List<E> readExcell(MultipartFile multipartFile) throws IOException{
		

		List<E> list = new LinkedList<>();
		AnalysisEventListener<E> listener = new AnalysisEventListener<E>() {

			@Override
			public void invoke(E object, AnalysisContext context) {
				// TODO Auto-generated method stub
				list.add(object);
			}

			@Override
			public void doAfterAllAnalysed(AnalysisContext context) {
				// TODO Auto-generated method stub
				
			}
			
		};
	

		InputStream  inputStream = multipartFile.getInputStream();
		Sheet sheet = new Sheet(1, 0,this.getEntityClass());   
         ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, listener);
         excelReader.read(sheet);
         inputStream.close();
	    return list;
		 
	}
	
	@ApiOperation("基础添加或修改")
	@PostMapping("/save")
	@RbacMapping("修改")
	public EntityVo<E> save(@Validated @RequestBody EntityPo<E> po) {
		E entity = po.getEntity();
		this.superService.save(entity);
		EntityVo<E> vo =new EntityVo<>();
		vo.setEntity(entity);
		vo.setMsg("修改数据成功");
		return vo;
	}
	  
	
	@ApiOperation("基础删除")
	@PostMapping("/delete")
	@RbacMapping("删除")
	public EntityVo<E> delete(@Validated @RequestBody EntityPo<E> po) {
		E entity = po.getEntity();
		this.superService.delete(entity);
		EntityVo<E> vo =new EntityVo<>();
		vo.setEntity(entity);
		vo.setMsg("删除数据成功");
		return vo;
	}
	
	
	@ApiOperation("基础查询")
	@PostMapping("/page")
	@RbacMapping(value="查询",save=false)
	public PageVo<E> page(@RequestBody PagePo<E> params) {
		Page<E> page = this.superService.page(params);
		PageVo<E> vo =new PageVo<>();
		vo.setPage(page);
		vo.setMsg("查询数据成功");
		return vo;
	}
	
	
	public Class<E> getEntityClass() {
		return (Class<E>)GenericUtil.getGeneric(this.getClass(), SuperController.class, 0);
	}
	
}
